home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / raytrace / pov / gen / accel / accel.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  8KB  |  364 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #define TRUE   1
  7. #define FALSE  0
  8.  
  9. typedef struct
  10. {
  11.    double x;
  12.    double y;
  13.    double z;
  14. } Vector;
  15.  
  16. char *KeyWords[] =
  17. {
  18.    "ACCELERATION","FRAMESPERSECOND","TOTALFRAMES","STARTPOSITION",
  19.    "STARTVELOCITY","BOUNCEX","BOUNCEY","BOUNCEZ","ACCELP"
  20. };
  21.  
  22. enum KeyTokens
  23. {
  24.    ACCEL_TOK,FPS_TOK,TOTFRAMES_TOK,STARTPOS_TOK,STARTVEL_TOK,
  25.    BOUNCEX_TOK,BOUNCEY_TOK,BOUNCEZ_TOK,ACCELP_TOK,ERROR_TOK
  26. };
  27.  
  28.  
  29. double VectorLength(Vector *CurVector);
  30. void VectorAdd(Vector *Vector1, Vector *Vector2);
  31. void VectorMultiply(Vector *Vector1, double Factor);
  32. void VectorMake(Vector *Direction, double Length);
  33. void VectorCopy(Vector *Vector1, Vector *Vector2);
  34. int FindToken(char *TokString);
  35.  
  36. Vector CurrentAcceleration;
  37. Vector CurrentVelocity;
  38. Vector CurrentPosition;
  39. Vector AccelerationVector;
  40. Vector PointAccelVector;
  41.  
  42. double BounceY,BounceX,BounceZ,BounceXFac,BounceYFac,BounceZFac;
  43. int DoBounceX,DoBounceY,DoBounceZ;
  44. int InitBounceX,InitBounceY,InitBounceZ;
  45.  
  46. double Acceleration,PointAccel,PointAccelRad,FramesPerSecond;
  47.  
  48. int TotalFrames, CurrentFrame,SkipFrames;
  49.  
  50. main(int argc, char *argv[])
  51. {
  52.    Vector TempVector1,TempVector2;
  53.  
  54.    PointAccelRad = 0.0;
  55.  
  56.    if (!LoadScript(argv[1]))
  57.    {
  58.       printf("; Unable to load script %s\n",argv[1]);
  59.       exit(1);
  60.    }
  61.  
  62.    SkipFrames = 0;
  63.  
  64.    if (argc > 2)
  65.       SkipFrames = atoi(argv[2]);
  66.  
  67.    if (!SkipFrames)
  68.       SkipFrames = 1;
  69.  
  70.    VectorCopy(&CurrentAcceleration,&AccelerationVector);
  71.  
  72.    printf("; Generated by ACCEL, by John M. Trindle June 1991\n");
  73.  
  74.    printf("%lf %lf %lf\n",CurrentPosition.x,
  75.                           CurrentPosition.y,
  76.                           CurrentPosition.z);
  77.  
  78.    for (CurrentFrame = 1; CurrentFrame < TotalFrames*SkipFrames; CurrentFrame++)
  79.    {
  80.       if (PointAccelRad != 0.0)
  81.       {
  82.          CreateCurrentAccel();
  83.       }
  84.  
  85.       VectorCopy(&TempVector1,&CurrentVelocity);
  86.       VectorCopy(&TempVector2,&CurrentAcceleration);
  87.       VectorMultiply(&TempVector2,1.0/FramesPerSecond);
  88.  
  89.       VectorAdd(&CurrentVelocity,&TempVector2);
  90.       VectorCopy(&TempVector2,&CurrentVelocity);
  91.       VectorAdd(&TempVector1,&TempVector2);
  92.       VectorMultiply(&TempVector1,0.5/FramesPerSecond);
  93.  
  94.       VectorAdd(&CurrentPosition,&TempVector1);
  95.       if (DoBounceX)
  96.       {
  97.          if (BounceNumX() != InitBounceX)
  98.          {
  99.             CurrentPosition.x = BounceX;
  100.             CurrentVelocity.x = -CurrentVelocity.x*BounceXFac;
  101.          }
  102.       }
  103.       if (DoBounceY)
  104.       {
  105.          if (BounceNumY() != InitBounceY)
  106.          {
  107.             CurrentPosition.y = BounceY;
  108.             CurrentVelocity.y = -CurrentVelocity.y*BounceYFac;
  109.          }
  110.       }
  111.       if (DoBounceZ)
  112.       {
  113.          if (BounceNumZ() != InitBounceZ)
  114.          {
  115.             CurrentPosition.z = BounceZ;
  116.             CurrentVelocity.z = -CurrentVelocity.z*BounceZFac;
  117.          }
  118.       }
  119.       if (!(CurrentFrame % SkipFrames))
  120.       {
  121.          printf("%lf %lf %lf\n",CurrentPosition.x,
  122.                                 CurrentPosition.y,
  123.                                 CurrentPosition.z);
  124.       }
  125.  
  126.    }
  127. }
  128.  
  129.  
  130.  
  131.  
  132. double VectorLength(Vector *CurVector)
  133. {
  134.    double RetVal;
  135.  
  136.    RetVal = sqrt(CurVector->x*CurVector->x+
  137.                  CurVector->y*CurVector->y+
  138.                  CurVector->z*CurVector->z);
  139.  
  140.    return(RetVal);
  141. }
  142.  
  143. void VectorAdd(Vector *Vector1, Vector *Vector2)
  144. {
  145.    Vector1->x += Vector2->x;
  146.    Vector1->y += Vector2->y;
  147.    Vector1->z += Vector2->z;
  148. }
  149.  
  150. void VectorMake(Vector *Direction, double Length)
  151. {
  152.    double DirLength;
  153.  
  154.    DirLength = VectorLength(Direction);
  155.  
  156.    Direction->x *= (Length/DirLength);
  157.    Direction->y *= (Length/DirLength);
  158.    Direction->z *= (Length/DirLength);
  159. }
  160.  
  161. void VectorCopy(Vector *Vector1, Vector *Vector2)
  162. {
  163.    Vector1->x = Vector2->x;
  164.    Vector1->y = Vector2->y;
  165.    Vector1->z = Vector2->z;
  166. }
  167.  
  168. void VectorMultiply(Vector *Vector1, double Factor)
  169. {
  170.    Vector1->x *= (Factor);
  171.    Vector1->y *= (Factor);
  172.    Vector1->z *= (Factor);
  173. }
  174.  
  175. int LoadScript(char *ScriptName)
  176. {
  177.    FILE *ScrFP;
  178.  
  179.    char *Token,*ParamString,LineBuffer[132];
  180.  
  181.    int CurTok;
  182.  
  183.    DoBounceX = DoBounceY = DoBounceZ = FALSE;
  184.  
  185.    ScrFP = fopen(ScriptName,"r");
  186.  
  187.    if (ScrFP == NULL)
  188.       return(FALSE);
  189.  
  190.    while (!feof(ScrFP))
  191.    {
  192.       fgets(LineBuffer,132,ScrFP);
  193.       if (LineBuffer[strlen(LineBuffer)-1] == '\n')
  194.             LineBuffer[strlen(LineBuffer)-1] = '\0';
  195.  
  196.       if (*LineBuffer == ';')
  197.          continue;
  198.  
  199.       Token = strtok(LineBuffer," \t");
  200.  
  201.       ParamString = LineBuffer+strlen(Token)+1;
  202.  
  203.       CurTok = FindToken(Token);
  204.  
  205.       switch(CurTok)
  206.       {
  207.          case ACCEL_TOK:
  208.             sscanf(ParamString,"%lf %lf %lf %lf",&Acceleration,
  209.                &(AccelerationVector.x),
  210.                &(AccelerationVector.y),
  211.                &(AccelerationVector.z));
  212.             VectorMake(&AccelerationVector,Acceleration);
  213.             break;
  214.  
  215.          case FPS_TOK:
  216.             sscanf(ParamString,"%lf",&FramesPerSecond);
  217.             break;
  218.  
  219.          case TOTFRAMES_TOK:
  220.             sscanf(ParamString,"%d",&TotalFrames);
  221.             break;
  222.  
  223.          case STARTPOS_TOK:
  224.             sscanf(ParamString,"%lf %lf %lf",
  225.                &(CurrentPosition.x),
  226.                &(CurrentPosition.y),
  227.                &(CurrentPosition.z));
  228.             break;
  229.  
  230.          case STARTVEL_TOK:
  231.             sscanf(ParamString,"%lf %lf %lf",
  232.                &(CurrentVelocity.x),
  233.                &(CurrentVelocity.y),
  234.                &(CurrentVelocity.z));
  235.             break;
  236.  
  237.          case BOUNCEX_TOK:
  238.             DoBounceX = TRUE;
  239.             sscanf(ParamString,"%lf %lf",&BounceX,&BounceXFac);
  240.             break;
  241.  
  242.          case BOUNCEY_TOK:
  243.             DoBounceY = TRUE;
  244.             sscanf(ParamString,"%lf %lf",&BounceY,&BounceYFac);
  245.             break;
  246.  
  247.          case BOUNCEZ_TOK:
  248.             DoBounceZ = TRUE;
  249.             sscanf(ParamString,"%lf %lf",&BounceZ,&BounceZFac);
  250.             break;
  251.  
  252.          case ACCELP_TOK:
  253.             sscanf(ParamString,"%lf %lf %lf %lf %lf",&PointAccel,&PointAccelRad,
  254.                &(PointAccelVector.x),
  255.                &(PointAccelVector.y),
  256.                &(PointAccelVector.z));
  257.  
  258.             break;
  259.  
  260.       }
  261.    }
  262.  
  263.    if (DoBounceX)
  264.       InitBounceX = BounceNumX();
  265.  
  266.    if (DoBounceY)
  267.       InitBounceY = BounceNumY();
  268.  
  269.    if (DoBounceZ)
  270.       InitBounceZ = BounceNumZ();
  271.  
  272.    fclose(ScrFP);
  273.  
  274.    return(TRUE);
  275. }
  276.  
  277. int FindToken(char *TokString)
  278. {
  279.    int RetVal,i;
  280.  
  281.    for (i = 0; i < ERROR_TOK; i++)
  282.    {
  283.       if (!stricmp(TokString,KeyWords[i]))
  284.          break;
  285.    }
  286.    RetVal = i;
  287.    return(RetVal);
  288. }
  289.  
  290. int BounceNumX()
  291. {
  292.   
  293.    int RetVal;
  294.  
  295.    if (CurrentPosition.x > BounceX)
  296.       RetVal = 1;
  297.  
  298.    if (CurrentPosition.x < BounceX)
  299.       RetVal = 0;
  300.  
  301.    if (CurrentPosition.x == BounceX)
  302.       RetVal = -1;
  303.    
  304.    return(RetVal);
  305. }
  306.  
  307. int BounceNumY()
  308. {
  309.   
  310.    int RetVal;
  311.  
  312.    if (CurrentPosition.y > BounceY)
  313.       RetVal = 1;
  314.  
  315.    if (CurrentPosition.y < BounceY)
  316.       RetVal = 0;
  317.  
  318.    if (CurrentPosition.y == BounceY)
  319.       RetVal = -1;
  320.    
  321.    return(RetVal);
  322. }
  323.  
  324. int BounceNumZ()
  325. {
  326.   
  327.    int RetVal;
  328.  
  329.    if (CurrentPosition.z > BounceZ)
  330.       RetVal = 1;
  331.  
  332.    if (CurrentPosition.z < BounceZ)
  333.       RetVal = 0;
  334.  
  335.    if (CurrentPosition.z == BounceZ)
  336.       RetVal = -1;
  337.    
  338.    return(RetVal);
  339. }
  340.  
  341.  
  342. CreateCurrentAccel()
  343. {
  344.    double Distance,AccelMag;
  345.  
  346.    CurrentAcceleration.x =  PointAccelVector.x - CurrentPosition.x;
  347.    CurrentAcceleration.y =  PointAccelVector.y - CurrentPosition.y;
  348.    CurrentAcceleration.z =  PointAccelVector.z - CurrentPosition.z;
  349.  
  350.    Distance = VectorLength(&CurrentAcceleration);
  351.  
  352.    if (Distance > 0.0)
  353.       AccelMag = (PointAccelRad/Distance)*(PointAccelRad/Distance);
  354.    else
  355.       AccelMag = 0.0;
  356.  
  357.    AccelMag *= PointAccel;
  358.  
  359.    VectorMake(&CurrentAcceleration,AccelMag);
  360.  
  361.    VectorAdd(&CurrentAcceleration,&AccelerationVector);
  362.  
  363. }
  364.